<html>
<head>
<script src="../../lib/OpenLayers.js"></script>
  <script type="text/javascript">

    function test_Elements_constructor(t) {
        t.plan(5);
        var el = document.createElement('div');
        document.body.appendChild(el);
        el.id = "foo";
        
        OpenLayers.Renderer.Elements.prototype._createRenderRoot =
            OpenLayers.Renderer.Elements.prototype.createRenderRoot;
        
        var rendererRoot = document.createElement("div");
        OpenLayers.Renderer.Elements.prototype.createRenderRoot = function() {
            return rendererRoot;
        };
        
        OpenLayers.Renderer.Elements.prototype._createRoot =
            OpenLayers.Renderer.Elements.prototype.createRoot;
        
        var root = document.createElement("div");
        OpenLayers.Renderer.Elements.prototype.createRoot = function() {
            return root;
        };
        
        var r = new OpenLayers.Renderer.Elements("foo");
        
        t.ok(r instanceof OpenLayers.Renderer.Elements, "new OpenLayers.Renderer.Elements returns Elements object" );
        t.ok(r.rendererRoot != null, "elements rendererRoot is not null");
        t.ok(r.root != null, "elements root is not null");
        
        t.ok(r.root.parentNode == rendererRoot, "elements root is correctly appended to rendererRoot");
        t.ok(r.rendererRoot.parentNode == el, "elements rendererRoot is correctly appended to container");
        
        OpenLayers.Renderer.Elements.prototype.createRenderRoot =
            OpenLayers.Renderer.Elements.prototype._createRenderRoot;
        OpenLayers.Renderer.Elements.prototype.createRoot =
            OpenLayers.Renderer.Elements.prototype._createRoot;
    }
    
    function test_Elements_destroy(t) {
        t.plan(5);
        
        OpenLayers.Renderer.Elements.prototype._initialize =
            OpenLayers.Renderer.Elements.prototype.initialize;
        
        OpenLayers.Renderer.Elements.prototype.initialize = function() {}
        
        var g_Clear = false, g_Destroy = false;
        
        OpenLayers.Renderer.prototype._destroy = 
            OpenLayers.Renderer.prototype.destroy;
            
        OpenLayers.Renderer.prototype.destroy = function() {
            g_Destroy = true;
        };
        
        var r = new OpenLayers.Renderer.SVG(document.body);
        r.clear = function() {
            g_Clear = true;
        };
        r.rendererRoot = 'foo';
        r.root = 'bar';
        r.xmlns = 'dude';
        
        r.destroy();
        
        t.eq(g_Clear, true, "OpenLayers.Renderer.Elements.clear() called");
        t.eq(r.rendererRoot, null, "rendererRoot nullified");
        t.eq(r.root, null, "root nullified");
        t.eq(r.xmlns, null, "xmlns nullified");
        t.eq(g_Destroy, true, "OpenLayers.Renderer.destroy() called");
        
        OpenLayers.Renderer.Elements.prototype.initialize =
            OpenLayers.Renderer.Elements.prototype._initialize;
        OpenLayers.Renderer.prototype.destroy = 
            OpenLayers.Renderer.prototype._destroy;
    }
    
    function test_Elements_clear(t) {
        t.plan(1);
        
        OpenLayers.Renderer.Elements.prototype._initialize =
            OpenLayers.Renderer.Elements.prototype.initialize;
        
        OpenLayers.Renderer.Elements.prototype.initialize = function() {}
        
        var r = new OpenLayers.Renderer.Elements();
        var element = document.createElement("div");
        r.root = element;
        
        var node = document.createElement("div");
        element.appendChild(node);
        
        r.clear();
        
        t.ok(r.root.childNodes.length == 0, "root is correctly cleared");
        
        OpenLayers.Renderer.Elements.prototype.initialize =
            OpenLayers.Renderer.Elements.prototype._initialize;
    }
    
    function test_Elements_drawGeometry(t) {
        t.plan(5);
        
        OpenLayers.Renderer.Elements.prototype._initialize =
            OpenLayers.Renderer.Elements.prototype.initialize;
        
        OpenLayers.Renderer.Elements.prototype.initialize = function() {};
        
        var r = new OpenLayers.Renderer.Elements();
        
        var element = document.createElement("div");
        r.root = element;

        r.nodeFactory = function(id, type) {
            var element = document.createElement("div");
            return element;
        };
        var g_Node = null;
        r.drawGeometryNode = function(node, geometry, style) {
            g_Node = node;
            return node;
        };
        r.getNodeType = function(geometry, style) {
            return "div";
        };
        geometry = {
            id: 'foo',
            CLASS_NAME: 'bar'
        };
        var style = true;
        var featureId = 'dude';
        r.drawGeometry(geometry, style, featureId);
        t.ok(g_Node.parentNode == r.root, "node is correctly appended to root");
        t.eq(g_Node._featureId, 'dude', "_featureId is correct");
        t.ok(g_Node._style, "_style is correct");
        t.eq(g_Node._geometryClass, 'bar', "_geometryClass is correct");

        var _getElement = OpenLayers.Util.getElement;
        OpenLayers.Util.getElement = function(id) {
            return g_Node;
        }

        var style = {'display':'none'};
        r.drawGeometry(geometry, style, featureId);
        t.ok(g_Node.parentNode != r.root, "node is correctly removed");
       
        OpenLayers.Util.getElement = _getElement;
        OpenLayers.Renderer.Elements.prototype.initialize =
            OpenLayers.Renderer.Elements.prototype._initialize;
    }

    function test_Elements_drawGeometry_2(t) {
        t.plan(9);
        
        OpenLayers.Renderer.Elements.prototype._initialize =
            OpenLayers.Renderer.Elements.prototype.initialize;
            
        OpenLayers.Renderer.Elements.prototype.initialize = function() {};

        var r = new OpenLayers.Renderer.Elements();
        
        var element = document.createElement("div");
        r.root = element;
        
        r.nodeFactory = function(id, type) {
            var element = document.createElement("div");
            return element;
        };
        r.setStyle = function(node, style, options, geometry) {
            return node;
        };
        
        // point
        var properDraw = false;
        r.drawPoint = function(node, geometry) {
            properDraw = true;
            return {};
        };
        geometry = {CLASS_NAME: 'OpenLayers.Geometry.Point'};
        style = true;
        r.drawGeometry(geometry, style);
        t.ok(properDraw, "drawGeometry called drawPoint when passed a point");

        // line string
        var properDraw = false;
        r.drawLineString = function(g) {
            properDraw = true;
            return {};
        };
        geometry = {CLASS_NAME: 'OpenLayers.Geometry.LineString'};
        style = true;
        r.drawGeometry(geometry, style);
        t.ok(properDraw, "drawGeometry called drawLineString when passed a line string");

        // linear ring
        var properDraw = false;
        r.drawLinearRing = function(g) {
            properDraw = true;
            return {};
        };
        geometry = {CLASS_NAME: 'OpenLayers.Geometry.LinearRing'};
        style = true;
        r.drawGeometry(geometry, style);
        t.ok(properDraw, "drawGeometry called drawLinearRing when passed a linear ring");

        // polygon
        var properDraw = false;
        r.drawPolygon = function(g) {
            properDraw = true;
            return {};
        };
        geometry = {CLASS_NAME: 'OpenLayers.Geometry.Polygon'};
        style = true;
        r.drawGeometry(geometry, style);
        t.ok(properDraw, "drawGeometry called drawPolygon when passed a polygon");

        // surface
        var properDraw = false;
        r.drawSurface = function(g) {
            properDraw = true;
            return {};
        }
        geometry = {CLASS_NAME: 'OpenLayers.Geometry.Surface'};
        style = true;
        r.drawGeometry(geometry, style);
        t.ok(properDraw, "drawGeometry called drawSurface when passed a surface");

        // rectangle
        var properDraw = false;
        r.drawRectangle = function(g) {
            properDraw = true;
            return {};
        };
        geometry = {CLASS_NAME: 'OpenLayers.Geometry.Rectangle'};
        style = true;
        r.drawGeometry(geometry, style);
        t.ok(properDraw, "drawGeometry called drawRectangle when passed a rectangle");

        // multi-point
        var properDraw = false;
        r.drawPoint = function(g) {
            properDraw = true;
            return {};
        };
        geometry = {
            CLASS_NAME: 'OpenLayers.Geometry.MultiPoint',
            components: [{CLASS_NAME: 'OpenLayers.Geometry.Point'}]
        };
        style = true;
        r.drawGeometry(geometry, style);
        t.ok(properDraw, "drawGeometry called drawPoint when passed a multi-point");

        // multi-linestring
        var properDraw = false;
        r.drawLineString = function(g) {
            properDraw = true;
            return {};
        };
        geometry = {
            CLASS_NAME: 'OpenLayers.Geometry.MultiLineString',
            components: [{CLASS_NAME: 'OpenLayers.Geometry.LineString'}]
        };
        style = true;
        r.drawGeometry(geometry, style);
        t.ok(properDraw, "drawGeometry called drawLineString when passed a multi-linestring");

        // multi-polygon
        var properDraw = false;
        r.drawPolygon = function(g) {
            properDraw = true;
            return {};
        };
        geometry = {
            CLASS_NAME: 'OpenLayers.Geometry.MultiPolygon',
            components: [{CLASS_NAME: 'OpenLayers.Geometry.Polygon'}]
        };
        style = true;
        r.drawGeometry(geometry, style);
        t.ok(properDraw, "drawGeometry called drawPolygon when passed a multi-polygon");
        
        OpenLayers.Renderer.Elements.prototype.initialize =
            OpenLayers.Renderer.Elements.prototype._initialize;
    }
    
    function test_Elements_getfeatureidfromevent(t) {
        t.plan(2);
        
        var node = {
            _featureId: 'foo'
        };
        var event = {
            target: node
        };
        
        var id = OpenLayers.Renderer.Elements.prototype.getFeatureIdFromEvent(event);
        t.eq(id, 'foo', "returned id is correct when event with target is passed");
        
        var event = {
            srcElement: node
        };
        
        var id = OpenLayers.Renderer.Elements.prototype.getFeatureIdFromEvent(event);
        t.eq(id, 'foo', "returned id is correct when event with srcElement is passed");
    }
    
    function test_Elements_erasegeometry(t) {
        t.plan(4);
        
        var el = document.createElement('div');
        document.body.appendChild(el);
        el.id = 'bar';
        var geometry = {
            id: 'bar'
        };
        
        OpenLayers.Renderer.Elements.prototype.eraseGeometry(geometry);
        t.ok(el.parentNode != document.body, "element correctly removed");

        var el = document.createElement('div');
        document.body.appendChild(el);
        el.id = 'bar';
        var geometry = {
            CLASS_NAME: "OpenLayers.Geometry.MultiPoint",
            components: [{
                id: 'bar'
            }]
        };

        OpenLayers.Renderer.Elements.prototype.eraseGeometry(geometry);
        t.ok(el.parentNode != document.body, "geometry components correctly removed when passed a multipoint");
        
        var el = document.createElement('div');
        document.body.appendChild(el);
        el.id = 'bar';
        var geometry = {
            CLASS_NAME: "OpenLayers.Geometry.MultiLineString",
            components: [{
                id: 'bar'
            }]
        };
        
        OpenLayers.Renderer.Elements.prototype.eraseGeometry(geometry);
        t.ok(el.parentNode != document.body, "geometry components correctly removed when passed a multilinestring");
        
        var el = document.createElement('div');
        document.body.appendChild(el);
        el.id = 'bar';
        var geometry = {
            CLASS_NAME: "OpenLayers.Geometry.MultiPolygon",
            components: [{
                id: 'bar'
            }]
        };
        
        OpenLayers.Renderer.Elements.prototype.eraseGeometry(geometry);
        t.ok(el.parentNode != document.body, "geometry components correctly removed when passed a multipolygon");
    }    

  </script>
</head>
<body>
<div id="map" style="width:500px;height:550px"></div>
</body>
</html>
